package com.hxkj.config;

import com.hxkj.interceptor.AuthHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.*;

import javax.cache.annotation.CacheRemove;

/**
 * 拦截器注册
 *
 * @author chenwei
 * @version 1.0
 * @datetime 2017/2/28 17:55
 */
@Service("webMvcConfiguration")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private AuthHandlerInterceptor authHandlerInterceptor;

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(mvcTaskExecutor());
        configurer.setDefaultTimeout(30 * 1000L);
        super.configureAsyncSupport(configurer);
    }

    @Bean
    public ThreadPoolTaskExecutor mvcTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(200);
        executor.setQueueCapacity(40000);
        executor.setKeepAliveSeconds(60);
        executor.initialize();
        return executor;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 认证拦截器
        registry.addInterceptor(authHandlerInterceptor)
        // 如果不加拦截路径，默认拦截全部
//        .addPathPatterns("/userinfo/**") // 用户信息相关
//        .addPathPatterns("/loginHistory/**")  // 用户历史登录信息相关
        .addPathPatterns("/abc")
        ;
        super.addInterceptors(registry);
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true);
    }

}
